package com.tian.core.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("base")
@Results( {  
    @Result(name = "download", type = "stream",params = { "contentType", "${contentType}",
   		 "inputName", "inputStream", "contentDisposition", "attachment;filename=${downloadFile}", "bufferSize","4096" })
})
public class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8107283725566867400L;
	
	public static final String USERSESSION = "com.tian.usersession";
	public static final String DOWNLOAD = "download";
	protected static final int BUFFER_SIZE = 16 * 1024; 
	private String json;
	//private String base;
	private String downloadFile;
	private String contentType;
	private String inputPath;

	public Map<String, Object> getSession(){
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session;
	}
	
	public HttpServletRequest getRequest(){
		HttpServletRequest request;
		request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return request;
	}
	
	public HttpServletResponse getResponse(){
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("UTF-8");
		return resp;
	}
	
	public String ajax(Object object){
		try{
			JSONArray jsonArray = new JSONArray();
			String json;
			if(object instanceof String){
				json=(String)object;
			}else if( object instanceof Collection){
				jsonArray.addAll((Collection)object);
				json=jsonArray.toString();
			}else if( object instanceof JSONObject){
				json=((JSONObject)object).toString();
			}else if( object instanceof JSONArray){
				json=((JSONArray)object).toString();
			}else if( object instanceof Boolean){
				Boolean b = (Boolean)object;
				if(b){
					json = "true";
				}else{
					json = "false";
				}
			}else{
				JSONObject jsonObject = JSONObject.fromObject(object);
				json=jsonObject.toString();
			}
			HttpServletResponse resp = ServletActionContext.getResponse();
			resp.setContentType("application/json;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			Writer out = resp.getWriter();
			out.append(json);
			out.flush();
		}catch (Exception e) {
			System.out.println(e);
		}
		
		return NONE;
	}
	
	public String ajax(Object object, JsonConfig config){
		try{
			JSONArray jsonArray = new JSONArray();
			String json;
			if(object instanceof String){
				json=(String)object;
			}else if( object instanceof Collection){
				jsonArray.addAll((Collection)object,config);
				json=jsonArray.toString();
			}else{
				JSONObject jsonObject = JSONObject.fromObject(object,config);
				json=jsonObject.toString();
			}
			HttpServletResponse resp = getResponse();
			resp.setContentType("application/json;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			Writer out = resp.getWriter();
			out.append(json);
			out.flush();
		}catch (Exception e) {
			System.out.println(e);
		}
		
		return NONE;
	}
	
	public InputStream getInputStream() throws Exception {
		InputStream in = ServletActionContext.getServletContext()   
                .getResourceAsStream(inputPath);   
        return in;   
    }
	
	public String getBase() {
		return getRequest().getScheme() + "://"
		+ getRequest().getServerName() + ":" + getRequest().getServerPort()
		+ getRequest().getContextPath();
	}

	
	//getter & setter
	public String getJson() {
		return json;
	}
	
	public void setJson(String json) {
		this.json = json;
	}

	public String getDownloadFile() {
		return downloadFile;
	}

	public void setDownloadFile(String downloadFile) {
		this.downloadFile = downloadFile;
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

}
